package com.calvin.tingapp.widget;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

public class NavigationFrameLayout extends FrameLayout {

	public NavigationFrameLayout(Context context) {
		super(context);
	}

	public NavigationFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public NavigationFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	@Override
	public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
		super.addView(child, index, params);
		Log.i("DEBUG", "NavView:" + getChildCount());
		if (getChildCount() > 1) {
			View preView = getChildAt(getChildCount() - 2);
			ObjectAnimator.ofFloat(preView, "translationX", 0f, -getWidth()).setDuration(500).start();
			ObjectAnimator.ofFloat(child, "translationX", getWidth(), 0f).setDuration(500).start();
		}
	}

	public void back() {
		if (getChildCount() > 1) {
			View intView = getChildAt(getChildCount() - 2);
			View outView = getChildAt(getChildCount() - 1);
			ObjectAnimator.ofFloat(intView, "translationX", -getWidth(), 0f).setDuration(500).start();
			Animator animator = ObjectAnimator.ofFloat(outView, "translationX", 0f, getWidth()).setDuration(500);
			animator.addListener(new AnimatorListener() {

				@Override
				public void onAnimationStart(Animator animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationRepeat(Animator animation) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onAnimationEnd(Animator animation) {
					// TODO Auto-generated method stub
					NavigationFrameLayout.this.removeView((View) ((ObjectAnimator) animation).getTarget());
				}

				@Override
				public void onAnimationCancel(Animator animation) {
					// TODO Auto-generated method stub

				}
			});
			animator.start();
		}
	}
}
